﻿

using WeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using OneGulp.WeChat.MP.AdvancedAPIs;
using OneGulp.WeChat.MP;

namespace WeiXinPF.Templates
{
    public class TBasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 模版的物理路径
        /// </summary>
        public string tPath { get; set; }

        /// <summary>
        /// 首页的模版文件名称
        /// </summary>
        public string templateIndexFileName { get; set; }

        /// <summary>
        /// 列表页面的模版文件名称
        /// </summary>
        public string templateListFileName { get; set; }

        /// <summary>
        /// 详情页面的模版文件名称
        /// </summary>
        public string templateDetailName { get; set; }

        /// <summary>
        /// 微帐号wid
        /// </summary>
        public int wid { get; set; }

        /// <summary>
        /// 用户对应的openId
        /// </summary>
        public string openid { get; private set; }

        /// <summary>
        /// 初始化模版的错误信息
        /// </summary>
        public string errInitTemplates { get; set; }

        public TBasePage()
        {
            tPath = string.Empty;
            templateIndexFileName = string.Empty;
            templateListFileName = string.Empty;
            templateDetailName = string.Empty;
            errInitTemplates = string.Empty;

            wid = MyCommFun.RequestInt(Constants.ParameterWeiXinId);
            BLL.wx_userweixin wuwBll = new BLL.wx_userweixin();
            if (wid == 0)
            {
                errInitTemplates = Constants.MessageNoWeiXinId;
                return;
            }

            bool isExist = wuwBll.wxCodeLegal(wid);
            if (!isExist)
            {
                errInitTemplates = Constants.MessageAccountAbnormity;
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <param name="state"></param>
        /// <param name="targetUrl"></param>
        public void OAuth2BaseProc(Model.wx_userweixin model, string state, string targetUrl)
        {
            var code = MyCommFun.QueryString(Constants.ParameterCode);
            openid = MyCommFun.QueryString(Constants.ParameterOpenId);

            if (!string.IsNullOrEmpty(openid))
            {
                return;
            }

            if (string.IsNullOrEmpty(code))
            {
                if (string.IsNullOrEmpty(targetUrl))
                {
                    targetUrl = MyCommFun.getTotalUrl();
                }
                var authorizedUrl = OAuthApi.GetAuthorizeUrl(model.AppId,
                    targetUrl, state, OAuthScope.snsapi_base);
                Response.Redirect(authorizedUrl);
            }
            else
            {
                var tokenResult = OAuthApi.GetAccessToken(model.AppId,
                    model.AppSecret, code);
                var authUrl = string.Format("{0}&openid={1}",
                    Request.Url, tokenResult.openid);
                Response.Redirect(authUrl);
            }
        }
    }
}